package com.seal.commons.utils;


import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * ClassName:LocalDateTimeUtil <br/>
 * Func: LocalDateTime 工具类 <br/>
 * Date:     2019-04-23 16:10 <br/>
 *
 * @author yong.huang
 * @see
 * @since JDK 1.8
 */
public class LocalDateTimeUtil {

    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return localDateTime.format(formatter);
    }


    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);

    }

    public static long getNow(){
        return getTimestampOfDateTime(LocalDateTime.now());
    }


    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return instant.toEpochMilli();
    }
}
